package com.mars.basic.config;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.mars.common.utils.MarsConstant;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
@Configuration
public class GlobalInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String secretKey = request.getHeader("from");
        if(!StringUtils.isNotBlank(secretKey)|| MarsConstant.GATEWAY.equals(secretKey)){
            response.setContentType("application/json; charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.write("非法访问");
            return false;
        }
        return true;

    }
}
